package com.gwc;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = {"/CookieA"})
public class CookieA extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie1 = new Cookie("name", "gwc");
        // 设置持久化Cookie,会被保存到浏览器硬盘中，不会随着浏览器关闭而消失
        cookie1.setMaxAge(60); // 60秒
        // 设置携带该Cookie的访问路径，只有访问CookieB的时候，才携带该Cookie
        cookie1.setPath("/web03/CookieB");
        // 会话级Cookie,关闭浏览器cookie则失效，且访问该项目其他资源时，都会携带该cookie
        Cookie cookie2 = new Cookie("password", "123456");

        // 将Cookie放入到相应对象中
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);
    }
}
